package com.huhao.concurrency.example.singleton;

import com.huhao.concurrency.annoations.Recommend;
import com.huhao.concurrency.annoations.ThreadSafe;

/**
 * 通过枚举类实现：最安全
 * 线程安全+推荐
 */
@ThreadSafe
@Recommend
public class SingletonExample7 {
    //私有构造函数
    private SingletonExample7() {
    }

    public static SingletonExample7 getInstance() {
        return Singleton.INSTANCE.getInstance();
    }

    private enum Singleton {
        INSTANCE;

        private SingletonExample7 singleton;

        //JVM保证这个方法绝对只调用一次
        Singleton() {
            singleton = new SingletonExample7();
        }

        public SingletonExample7 getInstance() {
            return singleton;
        }

    }
}